package org.cfk.asyncdownloadtutorial.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.cfk.asyncdownloadtutorial.dto.AsyncDownloadJobDTO;
import org.cfk.asyncdownloadtutorial.entity.AsyncDownloadJob;
import org.cfk.asyncdownloadtutorial.mapper.AsyncDownloadJobMapper;
import org.cfk.asyncdownloadtutorial.service.AsyncDownloadJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class AsyncDownloadJobServiceImpl implements AsyncDownloadJobService {

    @Resource
    private  RedisTemplate<String, String> redisTemplate;
    @Resource
    private  KafkaTemplate<String, String> kafkaTemplate;
    @Autowired(required = false)
    private  AsyncDownloadJobMapper asyncDownloadJobMapper;

    @Override
    public AsyncDownloadJobDTO getById(Long id) {
        AsyncDownloadJob job = asyncDownloadJobMapper.selectById(id);
        if (job == null) {
            return null;
        }
        return AsyncDownloadJobDTO.builder()
                .id(job.getId())
                .worker(job.getWorker())
                .requestContent(job.getRequestContent())
                .fileTitle(job.getFileTitle())
                .status(job.getStatus())
                .filePath(job.getFilePath())
                .createTime(job.getCreateTime())
                .updateTime(job.getUpdateTime())
                .build();
    }

    @Override
    public boolean setDownloadGenerating(Long id) {
        AsyncDownloadJob job = new AsyncDownloadJob();
        job.setId(id);
        job.setStatus("GENERATING");
        job.setUpdateTime(LocalDateTime.now());
        return asyncDownloadJobMapper.updateById(job) > 0;
    }

    @Override
    public void setDownloadFail(Long id) {
        AsyncDownloadJob job = new AsyncDownloadJob();
        job.setId(id);
        job.setStatus("FAILED");
        job.setUpdateTime(LocalDateTime.now());
        asyncDownloadJobMapper.updateById(job);
    }

    @Override
    public Long createAsyncDownloadJob(String worker, String requestContent, String fileTitle) {
        AsyncDownloadJob job = new AsyncDownloadJob();
        job.setWorker(worker);
        job.setRequestContent(requestContent);
        job.setFileTitle(fileTitle);
        job.setStatus("PENDING");
        job.setCreateTime(LocalDateTime.now());
        job.setUpdateTime(LocalDateTime.now());
        asyncDownloadJobMapper.insert(job);
        return job.getId();
    }

    @Override
    public boolean setDownloadSuccess(Long id, String filePath) {
        AsyncDownloadJob job = new AsyncDownloadJob();
        job.setId(id);
        job.setStatus("SUCCESS");
        job.setFilePath(filePath);
        job.setUpdateTime(LocalDateTime.now());
        return asyncDownloadJobMapper.updateById(job) > 0;
    }
}